<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
require_once MODEL_PATH.'/MY_Model.php';
class Users_m extends MY_Model {

    function __construct($db=null) {
        parent::__construct($db);
    }

    // Get a user's salt
    function getSalt($email = '') {
        if (!empty($email)) {
            $this->db->select('salt');
            $query = $this->db->getwhere('users', array('email'=>$email));
            if ($query->num_rows() > 0) {
                $row = $query->row();
                return $row->salt;
            }
        }
        return FALSE;
    }

    function getUser($params) {
    	if(isset($params['id'])) {
    		$this->db->where('users.id', $params['id']);
    	}

    	if(isset($params['email'])) {
    		$this->db->where('LOWER(users.email)', strtolower($params['email']));
    	}

    	if(isset($params['role'])) {
    		$this->db->where('users.role', $params['role']);
    	}
    	$this->db->limit(1);
    	$query = $this->db->get('users');

    	return $query->row();
    }

	function getUsers($params = array())
    {
    	if(isset($params['name'])) $this->db->where("users.name like '%" . $params['name'] . "%'");
    	if(isset($params['email'])) $this->db->like('users.email', $params['email']);
    	if(isset($params['is_active']) && $params['is_active'] > -1) $this->db->where('users.is_active', $params['is_active']);
    	if(isset($params['role']) && $params['role']) $this->db->where('users.role', $params['role']);

    	if(isset($params['order'])) $this->db->order_by($params['order']);
	
    	if (!empty($params['count'])){
    		$this->db->select('count(1) as count');
    		$query = $this->db->get('users');
    		if ($query->num_rows() == 0){
    			return 0;
    		}else{
    			return $query->row()->count;
    		}
    	}else{
    		$this->db->select('users.*');
    		$query = $this->db->get('users');
    		return $query->result();
    	}
    }

    function countUsers($params = array())
    {
    	if(isset($params['active'])) $this->db->where('is_active', $params['active']);
    	if(isset($params['role'])) $this->db->where('role', $params['role']);
		return $this->db->count_all_results('users');
    }

    function getRoles() {
    	return array('user'=>'User',
    				 'staff'=>'Staff',
    				 'admin'=>'Admin');
    }

	function newUser($input = array())
    {
		$this->load->helper('date');

        $this->db->insert('users', array(
        	'email'				=> $input->email,
        	'password'			=> $input->password,
        	'salt'				=> $input->salt,
        	'name' 				=> $input->name,
        	'role' 				=> empty($input->role) ? 'user' : $input->role,
        	'is_active' 		=> 0,
        	'activation_code' 	=> $input->activation_code,
        	'created_on' 		=> now(),
        	'ip' 				=> $this->input->ip_address()
        ));

		return $this->db->insert_id();

	}

	function updateUser($id, $data) {
		return $this->db->update('users', $data, array('id' => $id));
	}

	function updateLastLogin($id) {
		$this->load->helper('date');
		$this->db->update('users', array('last_login' => now()), array('id' => $id));
	}

	function activateUser($id) {
		$this->db->update('users', array('is_active' => 1, 'activation_code' => ''), array('id' => $id));
		return ($this->db->affected_rows() > 0);
	}

	function deleteUser($id) {
		$this->db->delete('users', array('id'=>$id));
		return $this->db->affected_rows();
	}

	function getByRoles($roles = array())
    {
    	$strRoles = "";
    	foreach($roles as $role) {
    		if($strRoles == "") {
    			$strRoles .= "'$role'";
    		} else {
    			$strRoles .= ", '$role'";
    		}
    	}

    	$this->db->where("role in ($strRoles)");
    	$this->db->select('users.*');
    	$query = $this->db->get('users');

    	return $query->result();
    }


    /**
     * Get Users
     * Enter description here ...
     * @param unknown_type $role
     * @param unknown_type $addEmptyItem
     */
    function getUserByRoles($role="",$addEmptyItem=FALSE){
		$this->db->select('name');
		$this->db->from('users');
		if($role)
			$this->db->where('role',$role);
		$query = $this->db->get();
		$arr=array();
		if ($addEmptyItem)
			$arr[0] = "---";
		if($query->num_rows()){
			foreach($query->result() as $row){
				$arr[$row->name] = $row->name;
			}
		}
		return $arr;
    }

}

?>